<template>
  <div class="wrapper">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            clearable
            v-model="orderParams.query"
            @change="inputChange"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <el-table
        :data="tableData"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column
          type="index"
          label="#"
        > </el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
        > </el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格"
        > </el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <span
              v-if="scope.row.pay_status==='0'"
              class="no-pay"
            >未付款</span>
            <span
              v-else
              class="already-pay"
            >已付款</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
        > </el-table-column>
        <el-table-column label="下单时间"><template slot-scope="scope">
            {{(Number(scope.row.update_time)*1000)|dataFormat}}
          </template> </el-table-column>
        <el-table-column
          label="操作"
          width="200px"
        >
          <template>

            <el-tooltip
              class="item"
              effect="dark"
              content="修改订单地址"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="onEdit()"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="物流信息"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="success"
                icon="el-icon-location-information"
                size="mini"
                @click="showLogistics()"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="orderParams.pagenum"
        :page-sizes="[5, 10, 20, 25]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="修改地址"
      :visible.sync="dialogChangeAdress"
      width="50%"
      :close-on-click-modal="false"
      @close="onCancle()"
    >
      <el-form
        :model="changeParams"
        :rules="rules"
        label-width="100px"
        ref="changeForm"
      >
        <el-form-item
          label="省市区/县"
          prop="cityMsg"
        >
          <el-cascader
            :options="citydata"
            :value="changeParams.cityMsg"
            @change="selectChange"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item
          label="详细地址"
          prop="detailadress"
        >
          <el-input v-model="changeParams.detailadress" />
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogChangeAdress = false">取 消</el-button>
        <el-button
          type="primary"
          @click="onChangeCommit"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示物流进度的对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressVisible"
      width="50%"
    >
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :icon="activity.icon"
          :color="activity.color"
          :timestamp="activity.time"
        >
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import citydata from './citydata.js'
export default {
  data () {
    return {
      tableData: [],
      total: 0,
      orderParams: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      changeParams: {
        cityMsg: [],
        detailadress: ''
      },
      citydata: citydata,
      progressVisible: false,
      progressInfo: [],
      dialogChangeAdress: false,
      rules: {
        cityMsg: [
          { required: true, message: '请选择城市', trigger: 'blur' }
        ],
        detailadress: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getOrderData();
  },
  mounted () {

  },
  methods: {
    async getOrderData () {
      const { data: res } = await this.$http.get('orders', {
        params: this.orderParams
      })
      if (res.meta.status !== 200) return this.$message(res.meta.msg);
      this.tableData = res.data.goods;
      this.total = res.data.total;
    },
    async showProgressBox () {
      const { data: res } = await this.$http.get('/kuaidi/804909574412544580')

      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败！')
      }
      res.data.map((item, index) => {
        if (index === 0) {
          item.icon = 'el-icon-location-outline';
          item.color = '#0bbd87';
        }
        return item;
      });
      this.progressInfo = res.data;

      this.progressVisible = true;
      console.log(this.progressInfo)
    },
    handleSizeChange (pageSize) {
      this.orderParams.pagesize = pageSize;
      this.getOrderData();
    },
    handleCurrentChange (page) {
      this.orderParams.pagenum = page;
      this.getOrderData();
    },
    inputChange () {
      this.getOrderData();
    },
    onEdit () {
      this.dialogChangeAdress = true;
      this.$refs.changeForm.resetFields();
    },
    showLogistics () {
      this.showProgressBox();
    },
    onChangeCommit () {
      this.$refs.changeForm.validate(async (result) => {
        if (!result) return;
        this.dialogChangeAdress = false;
      });
    },
    selectChange () {

    },
    onCancle () {
      this.dialogChangeAdress = false;
    }
  }
}
</script>
<style lang="scss" scoped>
.no-pay {
  background: red;
  color: white;
  font-size: 12px;
  padding: 0 5px;
  border-radius: 5px;
  display: table-cell;
}
.already-pay {
  background: limegreen;
  color: white;
  font-size: 12px;
  padding: 0 5px;
  border-radius: 5px;
  display: table-cell;
}
.el-cascader {
  width: 100%;
}
</style>
